package com.xhd.config;

import java.io.File;
import java.io.FileInputStream;
import java.net.URL;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;
import java.util.jar.JarFile;

import com.xhd.base.log.Log;
import com.xhd.base.log.LogFactory;
import com.xhd.cache.CacheMgr;
import com.xhd.cache.UseCache;
import com.xhd.server.ServerConfig;
import com.xhd.xml.XMLSerializer;

public class BizObjFactory implements UseCache {
	private HashMap jm15 = new HashMap(1024);
	private static BizObjFactory jm16 = null;
	private static final Log jm9;
	static Class jm10;
	static Class jm17;

	static {
		Class tmp7_4 = jm10;
		if (tmp7_4 == null) {

			try {
				BizObjFactory.jm10 = Class
						.forName("com.sinocc.base.config.BizObjFactory");
			} catch (ClassNotFoundException localClassNotFoundException) {
				throw new NoClassDefFoundError(localClassNotFoundException
						.getMessage());
			}
		}
		jm9 = LogFactory.getLog(tmp7_4);
	}

	public static synchronized BizObjFactory getInstance() {
		if (jm16 == null) {
			jm16 = new BizObjFactory();
			jm16.jm11();
		}
		return jm16;
	}

	private BizObjFactory() {
		CacheMgr.registerCache(this);
	}

	public Class getBizClass(String paramString) {
		if (paramString == null)
			return null;
		return ((Class) this.jm15.get(paramString.toLowerCase()));
	}

	private void jm11() {
		try {
			XMLSerializer localXMLSerializer = null;
			String str1 = ServerConfig.getConfFilePath("action");
			File localFile = new File(str1);
			if ((!(localFile.exists())) && (!(localFile.isFile())))
				return;
			FileInputStream localFileInputStream = new FileInputStream(
					localFile);
			localXMLSerializer = new XMLSerializer(localFileInputStream, false);
			Vector localVector = localXMLSerializer
					.getCollection("pack-config");
			localFileInputStream.close();
			if ((localVector == null) || (localVector.size() < 1))
				return;
			int i = localVector.size();
			for (int j = 0; j < i; ++j) {
				Map localMap = (Map) localVector.get(j);
				String str2 = (String) localMap.get("pack");
				String str3 = (String) localMap.get("sample");
				jm12(str2, str3);
			}
			jm9.info("Load biz object class count: " + this.jm15.size());
		} catch (Exception localException) {
			jm9.fatal("Load pack config", localException);
		}
	}

	private void jm12(String paramString1, String paramString2) {
		try {
			Vector localVector = null;
			URL localURL = Class.forName(paramString2).getResource("");
			String str1 = localURL.getPath();
			jm9.debug("loadBizObjects: " + str1);
			if ("jar".equalsIgnoreCase(localURL.getProtocol())) {
				int i = str1.indexOf(33);
				if (i > 0) {
					str1 = str1.substring(5, i);
					localVector = jm13(new JarFile(str1), paramString1);
				}
			} else {
				String str2 = paramString1;
				int k = str1.indexOf(str2.replace('.', '/'));
				if (k > 0) {
					str1 = str1.substring(0, k + str2.length());
					localVector = jm13(new File(str1), str2);
				}
			}
			if ((localVector == null) || (localVector.size() <= 0))
				return;
			int j = localVector.size();
			for (int k = 0; k < j; ++k) {
				Class localClass = (Class) localVector.get(k);
				String str3 = jm14(localClass.getName());
				if (this.jm15.containsKey(str3))
					jm9.warn("Class " + str3 + " repeat!");
				else
					this.jm15.put(str3, localClass);
			}
		} catch (Throwable localThrowable) {
			jm9.error("loadBizObjects", localThrowable);
		}
	}

	private Vector jm13(File paramFile, String paramString) {
		jm9.debug("findBizClasses in: " + paramFile.getPath());
		Vector localVector1 = new Vector();
		localVector1.add(paramFile);
		Class tmp45_42 = jm17;
		if (tmp45_42 == null) {

			try {
				BizObjFactory.jm17 = Class
						.forName("com.sinocc.base.util.BusinessClass");
			} catch (ClassNotFoundException localClassNotFoundException) {
				throw new NoClassDefFoundError(localClassNotFoundException
						.getMessage());
			}
		}
		Class localClass1 = tmp45_42;
		char c = File.separatorChar;
		String str1 = c + paramString.replace('.', c);
		Vector localVector2 = new Vector(256);
		while (localVector1.size() > 0) {
			paramFile = (File) localVector1.remove(0);
			File[] arrayOfFile = paramFile.listFiles();
			if (arrayOfFile == null)
				continue;
			String str2 = paramFile.getPath();
			int i = str2.indexOf(str1);
			str2 = str2.substring(i + 1);
			str2 = str2.replace(c, '.');
			if (str2.equals("com.sinocc.util"))
				continue;
			if (str2.equals("com.sinocc.base"))
				continue;
			int j = arrayOfFile.length;
			for (int k = 0; k < j; ++k)
				if (arrayOfFile[k].isDirectory()) {
					localVector1.add(arrayOfFile[k]);
				} else {
					String str3 = arrayOfFile[k].getPath();
					int l = str3.length();
					if (l < 7)
						continue;
					if (!(".class".equals(str3.substring(l - 6).toLowerCase())))
						continue;
					if (str3.indexOf(36) > 0)
						continue;
					str3 = str3.substring(0, l - 6);
					i = str3.indexOf(str1);
					str3 = str3.substring(i + 1);
					str3 = str3.replace(c, '.');
					try {
						Class localClass2 = Class.forName(str3);
						if (localClass1.isAssignableFrom(localClass2))
							localVector2.add(localClass2);
					} catch (Throwable localThrowable) {
						if (ServerConfig.IsDebug)
							jm9.error(str3, localThrowable);
					}
				}
		}
		return localVector2;
	}

	private Vector jm13(JarFile paramJarFile, String paramString) {
		String str1 = null;
		Enumeration localEnumeration = paramJarFile.entries();
		boolean bool = false;
		Class tmp14_11 = jm17;
		if (tmp14_11 == null) {

			try {
				BizObjFactory.jm17 = Class
						.forName("com.sinocc.base.util.BusinessClass");
			} catch (ClassNotFoundException localClassNotFoundException) {
				throw new NoClassDefFoundError(localClassNotFoundException
						.getMessage());
			}
		}
		Class localClass1 = tmp14_11;
		Vector localVector = new Vector(64);
		while (localEnumeration.hasMoreElements()) {
			str1 = localEnumeration.nextElement().toString();
			str1 = str1.replace('/', '.');
			if (str1.startsWith("com.sinocc.util."))
				continue;
			if (str1.startsWith("com.sinocc.base."))
				continue;
			if (!(bool)) {
				bool = str1.equals(paramString);
			} else {
				if (!(str1.startsWith(paramString)))
					break;
				String str2 = str1;
				int i = str2.length();
				if (i < 7)
					continue;
				if (!(".class".equals(str2.substring(i - 6).toLowerCase())))
					continue;
				if (str2.indexOf(36) > 0)
					continue;
				str2 = str2.substring(0, i - 6);
				try {
					Class localClass2 = Class.forName(str2);
					if (localClass1.isAssignableFrom(localClass2))
						localVector.add(localClass2);
				} catch (Throwable localThrowable) {
					if (ServerConfig.IsDebug)
						jm9.error(str2, localThrowable);
				}
			}
		}
		return localVector;
	}

	private String jm14(String paramString) {
		int i = paramString.lastIndexOf(46);
		if (i >= 0)
			paramString = paramString.substring(i + 1);
		return paramString.toLowerCase();
	}

	public boolean clearCache() {
		this.jm15.clear();
		System.out.println("BizObjFactory.clearCache.");
		jm16 = null;
		return true;
	}

	public int cacheSize() {
		return this.jm15.size();
	}

	public Object cacheData() {
		return this.jm15;
	}
}
